package flc.ast.fragment;

import androidx.fragment.app.Fragment;

import android.view.View;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.DressUpActivity;
import flc.ast.databinding.FragmentDressUpBinding;
import flc.ast.databinding.FragmentTemplateBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

import com.bumptech.glide.Glide;
import com.stark.cartoonavatarmaker.lib.core.AvatarTemplate;
import com.stark.cartoonavatarmaker.lib.core.AvatarTemplateCategory;
import com.stark.cartoonavatarmaker.lib.core.AvatarTemplateProvider;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TemplateFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TemplateFragment extends BaseNoModelFragment<FragmentTemplateBinding> {

    private List<AvatarTemplate> listTemplate1, listTemplate2, listTemplate3, listTemplate4;
    private int selPosition1 = 0, selPosition2 = 0, selPosition3 = 0, selPosition4 = 0;

    @Override
    protected int onCreate() {
        return R.layout.fragment_template;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container1);
        mDataBinding.ivTemplateLast1.setOnClickListener(this);
        mDataBinding.ivTemplateNext1.setOnClickListener(this);
        mDataBinding.ivTemplateLast2.setOnClickListener(this);
        mDataBinding.ivTemplateNext2.setOnClickListener(this);
        mDataBinding.ivTemplateLast3.setOnClickListener(this);
        mDataBinding.ivTemplateNext3.setOnClickListener(this);
        mDataBinding.ivTemplateLast4.setOnClickListener(this);
        mDataBinding.ivTemplateNext4.setOnClickListener(this);
        mDataBinding.ivTemplateImg1.setOnClickListener(this);
        mDataBinding.ivTemplateImg2.setOnClickListener(this);
        mDataBinding.ivTemplateImg3.setOnClickListener(this);
        mDataBinding.ivTemplateImg4.setOnClickListener(this);
        mDataBinding.ivTemplateCollect1.setOnClickListener(this);
        mDataBinding.ivTemplateCollect2.setOnClickListener(this);
        mDataBinding.ivTemplateCollect3.setOnClickListener(this);
        mDataBinding.ivTemplateCollect4.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivTemplateLast1:
                if (selPosition1 == 0) {
                    return;
                }
                selPosition1--;
                Glide.with(mContext).load(listTemplate1.get(selPosition1).getImgUri()).into(mDataBinding.ivTemplateImg1);
                checkPosition1();
                break;
            case R.id.ivTemplateNext1:
                if (selPosition1 == (listTemplate1.size() - 1)) {
                    return;
                }
                selPosition1++;
                Glide.with(mContext).load(listTemplate1.get(selPosition1).getImgUri()).into(mDataBinding.ivTemplateImg1);
                checkPosition1();
                break;
            case R.id.ivTemplateLast2:
                if (selPosition2 == 0) {
                    return;
                }
                selPosition2--;
                Glide.with(mContext).load(listTemplate2.get(selPosition2).getImgUri()).into(mDataBinding.ivTemplateImg2);
                checkPosition2();
                break;
            case R.id.ivTemplateNext2:
                if (selPosition2 == (listTemplate2.size() - 1)) {
                    return;
                }
                selPosition2++;
                Glide.with(mContext).load(listTemplate2.get(selPosition2).getImgUri()).into(mDataBinding.ivTemplateImg2);
                checkPosition2();
                break;
            case R.id.ivTemplateLast3:
                if (selPosition3 == 0) {
                    return;
                }
                selPosition3--;
                Glide.with(mContext).load(listTemplate3.get(selPosition3).getImgUri()).into(mDataBinding.ivTemplateImg3);
                checkPosition3();
                break;
            case R.id.ivTemplateNext3:
                if (selPosition3 == (listTemplate3.size() - 1)) {
                    return;
                }
                selPosition3++;
                Glide.with(mContext).load(listTemplate3.get(selPosition3).getImgUri()).into(mDataBinding.ivTemplateImg3);
                checkPosition3();
                break;
            case R.id.ivTemplateLast4:
                if (selPosition4 == 0) {
                    return;
                }
                selPosition4--;
                Glide.with(mContext).load(listTemplate4.get(selPosition4).getImgUri()).into(mDataBinding.ivTemplateImg4);
                checkPosition4();
                break;
            case R.id.ivTemplateNext4:
                if (selPosition4 == (listTemplate4.size() - 1)) {
                    return;
                }
                selPosition4++;
                Glide.with(mContext).load(listTemplate4.get(selPosition4).getImgUri()).into(mDataBinding.ivTemplateImg4);
                checkPosition4();
                break;
            case R.id.ivTemplateImg1://古风
                DressUpActivity.start(mContext,listTemplate1.get(selPosition1).part);
                break;
            case R.id.ivTemplateImg2://华丽
                DressUpActivity.start(mContext,listTemplate2.get(selPosition2).part);
                break;
            case R.id.ivTemplateImg3://甜美
                DressUpActivity.start(mContext,listTemplate3.get(selPosition3).part);
                break;
            case R.id.ivTemplateImg4://校园
                DressUpActivity.start(mContext,listTemplate4.get(selPosition4).part);
                break;
            case R.id.ivTemplateCollect1:
                List<AvatarTemplate> listSp = AppSpUtils.getTemplateList();
                if (listTemplate1.get(selPosition1).isSelected()) {
                    if (listSp != null && listSp.size() != 0) {
                        for (AvatarTemplate template : listSp) {
                            if (template.equals(listTemplate1.get(selPosition1))) {
                                listSp.remove(listTemplate1.get(selPosition1));
                                break;
                            }
                        }
                    }
                    AppSpUtils.saveTemplateList(listSp);
                    listTemplate1.get(selPosition1).setSelected(false);
                    mDataBinding.ivTemplateCollect1.setImageResource(R.drawable.aweishouc);
                } else {
                    List<AvatarTemplate> listSave = new ArrayList<>();
                    listSave.add(listTemplate1.get(selPosition1));
                    if (listSp != null && listSp.size() != 0) {
                        listSp.addAll(listSave);
                        AppSpUtils.saveTemplateList(listSp);
                    } else {
                        AppSpUtils.saveTemplateList(listSave);
                    }
                    mDataBinding.ivTemplateCollect1.setImageResource(R.drawable.ayishoucang);
                    listTemplate1.get(selPosition1).setSelected(true);
                }
                break;
            case R.id.ivTemplateCollect2:
                List<AvatarTemplate> listSp2 = AppSpUtils.getTemplateList();
                if (listTemplate2.get(selPosition2).isSelected()) {
                    if (listSp2 != null && listSp2.size() != 0) {
                        for (AvatarTemplate template : listSp2) {
                            if (template.equals(listTemplate2.get(selPosition2))) {
                                listSp2.remove(listTemplate2.get(selPosition2));
                                break;
                            }
                        }
                    }
                    AppSpUtils.saveTemplateList(listSp2);
                    listTemplate2.get(selPosition2).setSelected(false);
                    mDataBinding.ivTemplateCollect2.setImageResource(R.drawable.aweishouc);
                } else {
                    List<AvatarTemplate> listSave = new ArrayList<>();
                    listSave.add(listTemplate2.get(selPosition2));
                    if (listSp2 != null && listSp2.size() != 0) {
                        listSp2.addAll(listSave);
                        AppSpUtils.saveTemplateList(listSp2);
                    } else {
                        AppSpUtils.saveTemplateList(listSave);
                    }
                    mDataBinding.ivTemplateCollect2.setImageResource(R.drawable.ayishoucang);
                    listTemplate2.get(selPosition2).setSelected(true);
                }
                break;
            case R.id.ivTemplateCollect3:
                List<AvatarTemplate> listSp3 = AppSpUtils.getTemplateList();
                if (listTemplate3.get(selPosition3).isSelected()) {
                    if (listSp3 != null && listSp3.size() != 0) {
                        for (AvatarTemplate template : listSp3) {
                            if (template.equals(listTemplate3.get(selPosition3))) {
                                listSp3.remove(listTemplate3.get(selPosition3));
                                break;
                            }
                        }
                    }
                    AppSpUtils.saveTemplateList(listSp3);
                    listTemplate3.get(selPosition3).setSelected(false);
                    mDataBinding.ivTemplateCollect3.setImageResource(R.drawable.aweishouc);
                } else {
                    List<AvatarTemplate> listSave = new ArrayList<>();
                    listSave.add(listTemplate3.get(selPosition3));
                    if (listSp3 != null && listSp3.size() != 0) {
                        listSp3.addAll(listSave);
                        AppSpUtils.saveTemplateList(listSp3);
                    } else {
                        AppSpUtils.saveTemplateList(listSave);
                    }
                    mDataBinding.ivTemplateCollect3.setImageResource(R.drawable.ayishoucang);
                    listTemplate3.get(selPosition3).setSelected(true);
                }
                break;
            case R.id.ivTemplateCollect4:
                List<AvatarTemplate> listSp4 = AppSpUtils.getTemplateList();
                if (listTemplate4.get(selPosition4).isSelected()) {
                    if (listSp4 != null && listSp4.size() != 0) {
                        for (AvatarTemplate template : listSp4) {
                            if (template.equals(listTemplate4.get(selPosition4))) {
                                listSp4.remove(listTemplate4.get(selPosition4));
                                break;
                            }
                        }
                    }
                    AppSpUtils.saveTemplateList(listSp4);
                    listTemplate4.get(selPosition4).setSelected(false);
                    mDataBinding.ivTemplateCollect4.setImageResource(R.drawable.aweishouc);
                } else {
                    List<AvatarTemplate> listSave = new ArrayList<>();
                    listSave.add(listTemplate4.get(selPosition4));
                    if (listSp4 != null && listSp4.size() != 0) {
                        listSp4.addAll(listSave);
                        AppSpUtils.saveTemplateList(listSp4);
                    } else {
                        AppSpUtils.saveTemplateList(listSave);
                    }
                    mDataBinding.ivTemplateCollect4.setImageResource(R.drawable.ayishoucang);
                    listTemplate4.get(selPosition4).setSelected(true);
                }
                break;
        }
    }

    @Override
    protected void initData() {
        List<AvatarTemplateCategory> listCategories = AvatarTemplateProvider.getCategories();
        //古风
        listTemplate1 = listCategories.get(0).templates;
        for (AvatarTemplate avatarTemplate : listTemplate1) {
            List<AvatarTemplate> listSp = AppSpUtils.getTemplateList();
            if (listSp != null && listSp.size() != 0) {
                for (AvatarTemplate template : listSp) {
                    if (template.equals(avatarTemplate)) {
                        avatarTemplate.setSelected(true);
                        break;
                    }
                }
            } else {
                break;
            }
        }
        mDataBinding.tvTemplateName1.setText(listCategories.get(0).name);
        Glide.with(mContext).load(listTemplate1.get(0).getImgUri()).into(mDataBinding.ivTemplateImg1);
        checkPosition1();
        //华丽
        listTemplate2 = listCategories.get(1).templates;
        for (AvatarTemplate avatarTemplate : listTemplate2) {
            List<AvatarTemplate> listSp = AppSpUtils.getTemplateList();
            if (listSp != null && listSp.size() != 0) {
                for (AvatarTemplate template : listSp) {
                    if (template.equals(avatarTemplate)) {
                        avatarTemplate.setSelected(true);
                        break;
                    }
                }
            } else {
                break;
            }
        }
        mDataBinding.tvTemplateName2.setText(listCategories.get(1).name);
        Glide.with(mContext).load(listTemplate2.get(0).getImgUri()).into(mDataBinding.ivTemplateImg2);
        checkPosition2();
        //甜美
        listTemplate3 = listCategories.get(2).templates;
        for (AvatarTemplate avatarTemplate : listTemplate3) {
            List<AvatarTemplate> listSp = AppSpUtils.getTemplateList();
            if (listSp != null && listSp.size() != 0) {
                for (AvatarTemplate template : listSp) {
                    if (template.equals(avatarTemplate)) {
                        avatarTemplate.setSelected(true);
                        break;
                    }
                }
            } else {
                break;
            }
        }
        mDataBinding.tvTemplateName3.setText(listCategories.get(2).name);
        Glide.with(mContext).load(listTemplate3.get(0).getImgUri()).into(mDataBinding.ivTemplateImg3);
        checkPosition3();
        //校园
        listTemplate4 = listCategories.get(3).templates;
        for (AvatarTemplate avatarTemplate : listTemplate4) {
            List<AvatarTemplate> listSp = AppSpUtils.getTemplateList();
            if (listSp != null && listSp.size() != 0) {
                for (AvatarTemplate template : listSp) {
                    if (template.equals(avatarTemplate)) {
                        avatarTemplate.setSelected(true);
                        break;
                    }
                }
            } else {
                break;
            }
        }
        mDataBinding.tvTemplateName4.setText(listCategories.get(3).name);
        Glide.with(mContext).load(listTemplate4.get(0).getImgUri()).into(mDataBinding.ivTemplateImg4);
        checkPosition4();
    }

    private void checkPosition1() {
        if (selPosition1 == 0) {
            mDataBinding.ivTemplateLast1.setImageResource(R.drawable.ashangyigew);
        } else {
            mDataBinding.ivTemplateLast1.setImageResource(R.drawable.ashangyigex);
        }
        if (selPosition1 == (listTemplate1.size() - 1)) {
            mDataBinding.ivTemplateNext1.setImageResource(R.drawable.axianyiyew);
        } else {
            mDataBinding.ivTemplateNext1.setImageResource(R.drawable.axiayigex);
        }
        if (listTemplate1.get(selPosition1).isSelected()){
            mDataBinding.ivTemplateCollect1.setImageResource(R.drawable.ayishoucang);
        }else {
            mDataBinding.ivTemplateCollect1.setImageResource(R.drawable.aweishouc);
        }
    }

    private void checkPosition2() {
        if (selPosition2 == 0) {
            mDataBinding.ivTemplateLast2.setImageResource(R.drawable.ashangyigew);
        } else {
            mDataBinding.ivTemplateLast2.setImageResource(R.drawable.ashangyigex);
        }
        if (selPosition2 == (listTemplate2.size() - 1)) {
            mDataBinding.ivTemplateNext2.setImageResource(R.drawable.axianyiyew);
        } else {
            mDataBinding.ivTemplateNext2.setImageResource(R.drawable.axiayigex);
        }
        if (listTemplate2.get(selPosition2).isSelected()){
            mDataBinding.ivTemplateCollect2.setImageResource(R.drawable.ayishoucang);
        }else {
            mDataBinding.ivTemplateCollect2.setImageResource(R.drawable.aweishouc);
        }
    }

    private void checkPosition3() {
        if (selPosition3 == 0) {
            mDataBinding.ivTemplateLast3.setImageResource(R.drawable.ashangyigew);
        } else {
            mDataBinding.ivTemplateLast3.setImageResource(R.drawable.ashangyigex);
        }
        if (selPosition3 == (listTemplate3.size() - 1)) {
            mDataBinding.ivTemplateNext3.setImageResource(R.drawable.axianyiyew);
        } else {
            mDataBinding.ivTemplateNext3.setImageResource(R.drawable.axiayigex);
        }
        if (listTemplate3.get(selPosition3).isSelected()){
            mDataBinding.ivTemplateCollect3.setImageResource(R.drawable.ayishoucang);
        }else {
            mDataBinding.ivTemplateCollect3.setImageResource(R.drawable.aweishouc);
        }
    }

    private void checkPosition4() {
        if (selPosition4 == 0) {
            mDataBinding.ivTemplateLast4.setImageResource(R.drawable.ashangyigew);
        } else {
            mDataBinding.ivTemplateLast4.setImageResource(R.drawable.ashangyigex);
        }
        if (selPosition4 == (listTemplate4.size() - 1)) {
            mDataBinding.ivTemplateNext4.setImageResource(R.drawable.axianyiyew);
        } else {
            mDataBinding.ivTemplateNext4.setImageResource(R.drawable.axiayigex);
        }
        if (listTemplate4.get(selPosition4).isSelected()){
            mDataBinding.ivTemplateCollect4.setImageResource(R.drawable.ayishoucang);
        }else {
            mDataBinding.ivTemplateCollect4.setImageResource(R.drawable.aweishouc);
        }
    }


}